package sv.gob.mtps.colectivas.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import sv.gob.mtps.colectivas.model.TbExpediente;
import sv.gob.mtps.colectivas.model.TbSolicitante;
import sv.gob.mtps.colectivas.model.TbSolicitud;
import sv.gob.mtps.colectivas.model.TbSolicitudSolicitante;
import sv.gob.mtps.colectivas.service.ExpedienteService;
import sv.gob.mtps.colectivas.service.FindService;
import sv.gob.mtps.colectivas.service.SolicitantesService;
import sv.gob.mtps.colectivas.service.SolicitudesService;

@Controller
public class SolicitudesController {
			
	@Autowired
	SolicitudesService solicitudesService;
	
	@Autowired
	SolicitantesService solicitantesService;
	
	@Autowired
	ExpedienteService expedienteService;
	
	@Autowired
	FindService findService;
	
	
	@RequestMapping(value = "/admin/showSolicitudes")
	public  String showFondos(Map<String, Object> map){
		TbExpediente tbExpediente = new TbExpediente();
		TbSolicitud tbSolicitud = new TbSolicitud();
		tbSolicitud.setTbExpediente(tbExpediente);
		map.put("solicitud", tbSolicitud);
		map.put("tiposList", findService.listTipoSolictudes());
		map.put("causasList", findService.listCausaSolicitudes());
		map.put("solicitudesList", findService.listSolicitudes());
		return "admin/solicitudes";		
	}
	
	@RequestMapping(value="/admin/addSolicitud",method = RequestMethod.POST)
	public String addSolicitud(@ModelAttribute("solicitud") TbSolicitud tbSolicitud, BindingResult result ){
		
		TbExpediente tbExpediente = tbSolicitud.getTbExpediente();
		
		tbExpediente.setFechaExpediente(new Date());
		
		final Calendar c = Calendar.getInstance();    
        
        c.setTime(tbExpediente.getFechaExpediente());
        tbExpediente.setAnnoExpediente(c.get(Calendar.YEAR));
		
		
		
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	    String delegado = user.getUsername();
		
		tbExpediente.setDelegado(delegado);
				
		try{
			
			expedienteService.addExpediente(tbExpediente);
			tbSolicitud.setTbExpediente(tbExpediente);
			solicitudesService.addSolicitud(tbSolicitud);
		}
		catch(DataAccessException ex){
			ex.printStackTrace();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return "redirect:/admin/showSolicitudes";
	}
	
	@RequestMapping(value="/admin/editSolicitud",method = RequestMethod.POST)
	public String editSolicitud(@ModelAttribute("solicitud") TbSolicitud tbSolicitud, BindingResult result ){
		
		TbExpediente e = expedienteService.findExpediente(tbSolicitud.getTbExpediente().getIdExpediente());
		TbExpediente tbExpediente = tbSolicitud.getTbExpediente();
		e.setCorrelativoExpediente(tbExpediente.getCorrelativoExpediente());
		try{
			expedienteService.editExpediente(e);
			tbSolicitud.setTbExpediente(e);
			solicitudesService.updateSolicitud(tbSolicitud);
		}
		catch(DataAccessException ex){
			ex.printStackTrace();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return "redirect:/admin/showSolicitudes";
	}
	
	@RequestMapping(value="/admin/deleteSolicitud",method = RequestMethod.POST)
	public String deleteSolicitud(@ModelAttribute("solicitud") TbSolicitud tbSolicitud, BindingResult result ){
		
		try{
		
			solicitudesService.deleteSolicitud(tbSolicitud.getIdSolicitud());
		}
		catch(DataAccessException ex){
			ex.printStackTrace();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
		return "redirect:/admin/showSolicitudes";
	}
	
	@RequestMapping(value = "/admin/showSolicitantes")
	public  String showSolicitantes(@ModelAttribute("solicitud") TbSolicitud tbSolicitud,BindingResult result,Map<String, Object> map){
		TbSolicitante tbSolicitante = new TbSolicitante();
		tbSolicitante.setIdSolicitud(tbSolicitud.getIdSolicitud());
		
		map.put("solicitante", tbSolicitante);
		map.put("solicitantesList", findService.listSolicitudSolicitantes("from VwSolicitudSolicitante where id.idSolicitud="+tbSolicitud.getIdSolicitud()));
		
		return "admin/solicitantes";
	}
	
	@RequestMapping(value = "/admin/show_Solicitantes")
	public  String show_Solicitantes(@ModelAttribute("solicitante") TbSolicitante tbSolicitante,BindingResult result,Map<String, Object> map,HttpServletRequest request){
		try{
			String idSolicitud = null;
			
			 idSolicitud = (String) request.getParameter("idSolicitud");
			 if(idSolicitud == null){
				 idSolicitud = String.valueOf(tbSolicitante.getIdSolicitud());
			 }
		map.put("solicitantesList", findService.listSolicitudSolicitantes("from VwSolicitudSolicitante where id.idSolicitud="+idSolicitud));
		}
		catch(Exception ex){
			 ex.printStackTrace();
		 }
		return "admin/solicitantes";
	}
	
	@RequestMapping(value = "/admin/addSolicitante")
	public  String addSolicitante(@ModelAttribute("solicitante") TbSolicitante tbSolicitante,BindingResult result,Map<String, Object> map){
		if(!result.hasErrors()){
			TbSolicitudSolicitante ss = new TbSolicitudSolicitante();
			TbSolicitud tbSolicitud = solicitudesService.findSolicitud(tbSolicitante.getIdSolicitud());
			ss.setTbSolicitud(tbSolicitud);
			
			solicitantesService.addSolicitante(tbSolicitante);
			
			ss.setTbSolicitante(tbSolicitante);
			
			solicitantesService.addSolicitudSolicitante(ss);
			
			return "redirect:/admin/show_Solicitantes?idSolicitud="+tbSolicitante.getIdSolicitud();
		}
		
		return "redirect:/admin/showSolicitudes";
	}
	
	@RequestMapping(value = "/admin/editSolicitante")
	public  String editSolicitante(@ModelAttribute("solicitante") TbSolicitante tbSolicitante,BindingResult result,Map<String, Object> map){
		if(!result.hasErrors()){
			solicitantesService.editSolicitante(tbSolicitante);
			
			return "redirect:/admin/show_Solicitantes?idSolicitud="+tbSolicitante.getIdSolicitud();
		}
		
		return "redirect:/admin/showSolicitudes";
	}
	
	@RequestMapping(value = "/admin/deleteSolicitante")
	public  String deleteSolicitante(@ModelAttribute("solicitante") TbSolicitante tbSolicitante,BindingResult result,Map<String, Object> map){
		if(!result.hasErrors()){
			solicitantesService.deleteSolicitudSolicitante(tbSolicitante.getIdSolicitudSolicitante());
			
			solicitantesService.deleteSolicitante(tbSolicitante.getIdSolicitante());
			
			return "redirect:/admin/show_Solicitantes?idSolicitud="+tbSolicitante.getIdSolicitud();
		}
		
		return "redirect:/admin/showSolicitudes";
	}
	

}
